package com.qiaoxg.greendaodemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.qiaoxg.greendaodemo.Entity.DaoMaster;
import com.qiaoxg.greendaodemo.Entity.DaoSession;
import com.qiaoxg.greendaodemo.Entity.UserBean;
import com.qiaoxg.greendaodemo.Entity.UserBeanDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class DBManager {
    private final static String dbName = "test_db";
    private static DBManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;

    public DBManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static DBManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (DBManager.class) {
                if (mInstance == null) {
                    mInstance = new DBManager(context);
                }
            }
        }
        return mInstance;
    }

    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    /**
     * 插入一条记录
     *
     * @param UserBean
     */
    public void insertUser(UserBean UserBean) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserBeanDao userDao = daoSession.getUserBeanDao();
        userDao.insertOrReplace(UserBean);
    }

    public void insertUserList(List<UserBean> BlackInfoCallMIssedBeans) {
        if (BlackInfoCallMIssedBeans == null || BlackInfoCallMIssedBeans.isEmpty()) {
            return;
        }
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserBeanDao userDao = daoSession.getUserBeanDao();
        userDao.insertOrReplaceInTx(BlackInfoCallMIssedBeans);
    }

    public void deleteUser(UserBean UserBean) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserBeanDao userDao = daoSession.getUserBeanDao();
        userDao.queryBuilder()
                .where(UserBeanDao.Properties.Name.eq(UserBean.getName()))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities();
    }

    public void updateUser(UserBean UserBean) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserBeanDao userDao = daoSession.getUserBeanDao();
        userDao.update(UserBean);
    }

    public List<UserBean> queryUserList() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserBeanDao userDao = daoSession.getUserBeanDao();
        QueryBuilder<UserBean> qb = userDao.queryBuilder();
//                .orderDesc(UserBeanDao.Properties.Numbering);
        List<UserBean> list = qb.list();
        return list;
    }




}
